package com.sskj.wallet.ui.activity;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.AddressBean;
import com.sskj.lib.bean.CoinEnum;
import com.sskj.lib.model.room.AddressBeanViewModel;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.component.DaggerUserDataComponent;
import com.sskj.wallet.presenter.AddressBookActivityPresenter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.WALLET_ADDRESS_BOOK)//地址薄
public class AddressBookActivity extends BaseActivity<AddressBookActivityPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private SlimAdapter slimAdapter;
    private MVCCoolHelper<List<AddressBean>> mvcHelper;
    @Autowired
    boolean isSelect = false;
    @Inject
    AddressBeanViewModel addressBeanViewModel;
    List<AddressBean> addressBeans = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.wallet_activity_address_book;
    }

    @Override
    public AddressBookActivityPresenter getPresenter() {
        return new AddressBookActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        setTitle(App.INSTANCE.getString(R.string.wallet_addressBookActivity1_j));
        DaggerUserDataComponent.create().inject(this);
        setRightImage(R.mipmap.wallet_icon_menu, v -> {
            ARouter.getInstance().build(RConfig.WALLET_ADD_ADDRESS).navigation();
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 1))
                .setRightPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 30))
                .setLeftPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 30))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.wallet_bdc))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.wallet_recy_item_address_book, new SlimInjector<AddressBean>() {
            @Override
            public void onInject(AddressBean data, IViewInjector injector, List payloads) {

                injector
                        .image(R.id.ivCoin, CoinEnum.getByCode(data.getCode().toUpperCase()).getRes())
                        .text(R.id.tvName, data.getNotes())
                        .text(R.id.tvAddress, data.getCode().toUpperCase() + ":" + data.getAddress());
            }
        }).attachTo(recyclerView).updateData(new ArrayList());

        ModelRx2DataSource<AddressBean> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<AddressBean>)
                page -> {
                   return addressBeanViewModel.getUsersFlow();
                }, 1000);
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener((recyclerView1, i, view) -> {
                    if (isSelect) {
                        RxBus.getDefault().post((AddressBean) slimAdapter.getDataItem(i));
                        finish();
                    } else {
                        ARouter.getInstance().build(RConfig.WALLET_ADDRESS_EDIT)
                                .withSerializable(Constans.ADDRESS_BEAN, (AddressBean) slimAdapter.getDataItem(i))
                                .navigation();
                    }
                });
    }

    @Override
    protected void initData() {
//        addressBeanViewModel.getUsers().observe(this, addressBeans -> {
//            if (addressBeans != null) {
//                ArrayList<AddressBean> addressBeans1 = new ArrayList<>();
//                addressBeans1.addAll(addressBeans);
//                this.addressBeans=addressBeans1;
//                slimAdapter.updateData(this.addressBeans);
//            } else {
//                this.addressBeans = new ArrayList<>();
//                slimAdapter.updateData(new ArrayList());
//            }
//        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mvcHelper != null)
            mvcHelper.refresh();
    }


}
